# @Author: huguangzhi
# @design: E480
# @ContactEmail : huguangzhi@ucsdigital.com.com
# @ContactPhone : 13121961510
# @Desc  :
import sys
import time

import modbus_tk
import modbus_tk.defines as cst
from modbus_tk import modbus_tcp
import numpy as np


def main():
    """main"""

    logger = modbus_tk.utils.create_logger(name="console", record_format="%(message)s")

    try:
        #Create the server
        server = modbus_tcp.TcpServer(address="localhost", port=503)
        logger.info("running...")
        logger.info("enter 'quit' for closing the server")

        server.start()

        slave_1 = server.add_slave(1)
        slave_1.add_block('0', cst.HOLDING_REGISTERS, 0, 100)

        name = "0"
        address = 0

        while True:
            # arg = sys.stdin.readline().split(" ")
            # if arg[0] == "q":
            #     break
            # elif arg[0] == "set_values":
            #     values = []
            #     for item in arg[1:]:
            #         values.append(item)
            values = np.random.randint(10, size=10).tolist()
            slave = server.get_slave(1)
            slave.set_values(name, address, values)
            value = slave.get_values(name, address, len(values))
            sys.stdout.write('done: values written: %s\r\n' % str(value))
            time.sleep(1)


    finally:
        server.stop()






if __name__ == "__main__":
    main()
    "set_values 1 0 0 1 2 3"
